<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData = {}) {
      this.chart.setOption({
        color: ["#00f2f1", "#ed3f35"],
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: chartData.xAxis,
          // 去除刻度
          axisTick: {
            show: false
          },
          // 修饰刻度标签的颜色
          axisLabel: {
            color: "rgba(255,255,255,.7)"
          },
          // 去除x坐标轴的颜色
          axisLine: {
            show: false
          }
        },
        grid: {
          left: "10",
          top: "30",
          right: "10",
          bottom: "10",
          containLabel: true
        },
        tooltip: {
          trigger: "axis"
        },
        yAxis: {
          type: "value",
          // 去除刻度
          axisTick: {
            show: false
          },
          // 修饰刻度标签的颜色
          axisLabel: {
            color: "rgba(255,255,255,.7)"
          },
          // 修改y轴分割线的颜色
          splitLine: {
            lineStyle: {
              color: "#012f4a"
            }
          }
        },
        legend: {
          data: chartData.text,
          // 距离容器10%
          right: "10%",
          // 修饰图例文字的颜色
          textStyle: {
            color: "#4c9bfd"
          }
        },
        series: [{
          name: chartData.text[0],
          type: "line",
          stack: "总量",
          // 是否让线条圆滑显示
          smooth: true,
          data: chartData.in,
        },
        {
          name: chartData.text[1],
          type: "line",
          stack: "总量",
          smooth: true,
          data: chartData.out,
        }]
      })
    }
  }
}
</script>
